//
//  KCStartVC.m
//  Golden
//
//  Created by king on 2017/8/15.
//  Copyright © 2017年 KC. All rights reserved.
//

#import "KCStartVC.h"
#import "AppDelegate.h"

@interface KCStartVC ()<UIScrollViewDelegate>

@property (strong, nonatomic)UIPageControl *myPage;
@property (strong, nonatomic)UIScrollView *homeScroll;

@end

@implementation KCStartVC

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.KCNavView.hidden = YES;
    [self createStart];
}

- (void)dealloc {
    
//    [KCStation RevokeStation:_station];
    KCLog(@"♻️ Dealloc %@", NSStringFromClass([self class]));
}

#pragma mark - Setup

- (void)createStart{
    
    _homeScroll = [[UIScrollView alloc]initWithFrame:SCREEN_BOUNDS];
    _homeScroll.backgroundColor = WHITE_COLOR;
    _homeScroll.bounces = NO;
    _homeScroll.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT);
    _homeScroll.pagingEnabled = YES;
    _homeScroll.delegate = self;
    _homeScroll.showsVerticalScrollIndicator = NO;
    _homeScroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_homeScroll];
    
    _myPage = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.centerY, SCREEN_WIDTH, 20)];
    _myPage.currentPage = 0;
    _myPage.numberOfPages = 3;
    _myPage.pageIndicatorTintColor = [UIColor lightGrayColor];
    _myPage.currentPageIndicatorTintColor = [UIColor blackColor];
    [_myPage addTarget:self action:@selector(pageControlValueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_myPage];
    
    for (int i = 0 ; i < 3; i++) {
        
        UIImageView *mv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        mv.tag = START_TAG + i;
        mv.image = [UIImage imageNamed:[NSString stringWithFormat:@"login_bg_%d.png",i + 1]];
        mv.alpha = 0;
        [_homeScroll addSubview:mv];
        
        if (i == 0) {
            mv.alpha = 1;
        }
        
    }

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"登录" forState:UIControlStateNormal];
    btn.frame = CGRectMake(SCALE_SIZE(2) - 100, SCREEN_HEIGHT - 100, 200, 40);
    btn.backgroundColor = COLOR_FOR_RED;
    btn.titleLabel.font = SYSTEM_FONT(FONTS_14 + FONT_NUM);
    [btn addTarget:self action:@selector(loginAction)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

#pragma mark -Actions

- (void)loginAction{
    
    ShareApplicationDelegate.window.rootViewController = nil;
    KCTabBarVC *tabbar = [[KCTabBarVC alloc]init];
    KCNavigationVC *navi = [[KCNavigationVC alloc]initWithRootViewController:tabbar];
    ShareApplicationDelegate.window.rootViewController = navi;
    
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat xset = scrollView.contentOffset.x;

    UIImageView *mv = (UIImageView *)[self.view viewWithTag:START_TAG];
    UIImageView *mv1 = (UIImageView *)[self.view viewWithTag:START_TAG + 1];
    UIImageView *mv2 = (UIImageView *)[self.view viewWithTag:START_TAG + 2];
    
    if (xset <= SCREEN_WIDTH) {
        
        mv.alpha = (SCREEN_WIDTH - xset)/SCREEN_WIDTH;
        mv1.alpha = xset/SCREEN_WIDTH;
        mv1.left = xset;
        mv.left = xset;
        
    }else if (xset > SCREEN_WIDTH && xset <= SCREEN_WIDTH * 2){
        
        mv1.left = xset;
        mv1.alpha = (SCREEN_WIDTH * 2 - xset)/SCREEN_WIDTH * 2;
        mv2.alpha = (xset - SCREEN_WIDTH)/SCREEN_WIDTH;
        mv2.left = xset;
        
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat xset = scrollView.contentOffset.x;
    _myPage.currentPage = xset / SCREEN_WIDTH;
    
}

#pragma mark - pageControlValueChange

- (void)pageControlValueChange:(UIPageControl *)pageControl {
    
    CGFloat x_set = SCREEN_WIDTH * pageControl.currentPage;
    [_homeScroll scrollRectToVisible:CGRectMake(x_set, 0, SCREEN_WIDTH, SCREEN_HEIGHT) animated:YES];
    
}


@end
